exports.fight = function(player, req, resp, onHandled) {
    var isCash = req.args.cash ? true : false;
    var costCash = 0;

    var user = player.user;
    do {
        if( !isCash ) {
            var rankFreeLimit = gConfGlobal.RankDailyLimit;
            if( user.status.vip ) {
                rankFreeLimit += gConfVip[user.status.vip].RankDailyLimit;
            }
            if( user.rank.free >= rankFreeLimit ) {
                resp.code = 1; resp.desc = 'no more free fight'; break;
            }
        }else {
            var confBuy = gConfBuy[user.rank.cash + 1];
            if( !confBuy ) {
                resp.code = 1; resp.desc = 'no more to buy'; break;
            }
                
            costCash = confBuy.CashRank;
            if( user.status.cash < costCash ) {
                resp.code = 1; resp.desc = 'not enough cash'; break;
            }
        }
    }while(false);

    if( resp.code == 1 ) {
        onHandled();
        return;
    }

    requestWorld(req, resp, function(){
        if( resp.code == 0 && !resp.data.update ) {

            if( !isCash ) {
                user.rank.free += 1; 
                player.markDirty('rank.free');
            }else {
                player.costResource('cash', costCash, user.rank.cash);
                user.rank.cash += 1; 
                player.markDirty('rank.cash');
            }

            resp.data.cash = -costCash; 

            var status = player.user.status;
            status.gold += resp.data.gold;
            status.honor += resp.data.honor;

            player.markDirty('status.gold');
            player.markDirty('status.honor');
            
            resp.daily_task = gConfDailyTask.ChallengeRank;
            player.doDailyTask(gConfDailyTask.ChallengeRank);
            player.doTask('FightRank','all');
        }
        onHandled();
    });
}

exports.get = function(player, req, resp, onHandled) {
    var user = player.user;
    player.calculateTeamRoleAttr();

    var roles = {};
    var fightForceSum = 0;
    for( var pos in user.team ) {
        var rid = user.team[pos];
        if( !rid ) {
            continue;
        }

        var fightForce = user.role[rid].fight_force;
        roles[rid] = fightForce;
        fightForceSum += fightForce;
    }

    req.args.roles = roles;
    req.args.fight_force = fightForceSum;

    requestWorld(req, resp, function(){
        if( resp.code == 0 ) {
            resp.data.rank.free = user.rank.free;
            resp.data.rank.cash = user.rank.cash;
            resp.data.rank.have_reward = 0;    
            if( resp.data.rank.reward_rank && (user.rank.timer != resp.data.rank.reward_day) ) {
                resp.data.rank.have_reward = 1;    
            }
        }
        onHandled();
    });
}

exports.get_reward = function(player, req, resp, onHandled) {
    var user = player.user; 

    req.args.timer = user.rank.timer;

    requestWorld(req, resp, function(){
        if( resp.code == 0 ) {
            var rewardRank = resp.data.reward_rank;
            var rewardDay = resp.data.reward_day;
            if( rewardRank && (user.rank.timer != rewardDay) ) {
                var id = 1;
                for( ; ; id++ ) {
                    var rewardConf = gConfRank[id];
                    if( !rewardConf || rewardRank <= rewardConf.Rank) {
                        break;
                    }
                }

                if( !rewardConf ) {
                    id -= 1;
                }

                var rewardConf = gConfRank[id];
                var now = common.getTime();
                var week = Math.ceil((now - gConfGlobalServer.ServerStartTime)/86400/7);
                if( week >= 3 ) {
                    week = 3;
                }
                var awards = rewardConf['Awards'+week];
                resp.data.awards = player.addAwards(awards);

                user.rank.timer = rewardDay;
                player.markDirty('rank.timer');
            }else{
                resp.code = 1; resp.desc = 'got or not in rank';
            }
        }

        onHandled();
    });
}

exports.get_tower = function(player, req, resp, onHandled) {
    requestWorld(req, resp, onHandled);
}

exports.get_top = function(player, req, resp, onHandled) {
    requestWorld(req, resp, onHandled);
}

exports.get_user = function(player, req, resp, onHandled) {
    requestWorld(req, resp, onHandled);
}

exports.get_tv = function(player, req, resp, onHandled) {
    requestWorld(req, resp, onHandled);
}

exports.share_tv = function(player, req, resp, onHandled) {
    do {
        var content = req.args.content;
        var id = Math.floor(req.args.id);
        if ( typeof(content) != 'string' || content.trim().length == 0
            || content.length > 40 || isNaN(id) || id < 0 || id > 10000000 ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        if ( !verifyWordFilter(req.args.content, req.args.key) ){
            resp.code = 1; resp.desc = 'verify fail'; break;
        }

        var costCash = gConfGlobal.ShareBattleCash;
        if ( player.user.status.cash < costCash ) {
            resp.code = 1; resp.desc = 'not enough cash';break; 
        }
    }while(false);

    if( resp.code == 1 ) {
        onHandled();
        return;
    }

    requestWorld(req, resp, function(){
        if ( resp.code == 0 && resp.data.exist == 1 ) {
            player.costResource('cash', costCash);
        }

        onHandled();
    });
}

exports.get_topone2 = function(player, req, resp, onHandled) {
    if( player.user.status.level < gConfActivities.crazy.OpenLevel ) {
        resp.code = 1; resp.desc = 'level limit';
        onHandled();
        return;
    }

    requestWorld(req, resp, function(){
        if( resp.code == 0 ) {
            var topone2 = player.user.activity.topone2;
            if( resp.data.topone2.day != topone2.day ) {
                // 新的活动
                topone2.day = resp.data.topone2.day;
                topone2.vote = 0;

                player.markDirty('activity.topone2');
            }
            resp.data.vote = topone2.vote;
        }
        onHandled();
    });
}

exports.challenge_topone2 = function(player, req, resp, onHandled) {
    var user = player.user;
    req.args.day = user.activity.topone2.day;

    var cashCost = gConfGlobal.CrazyFightCashCost;
    if( user.status.cash < cashCost ) {
        resp.code = 1; resp.desc = 'not enough cash';
        onHandled();
        return;
    }

    requestWorld(req, resp, function(){
        if( resp.code == 0 ) {
            player.costResource('cash', cashCost);
            resp.data.cash = -cashCost;
        }
        onHandled();
    });
}

exports.get_topone2_reward = function(player, req, resp, onHandled) {
    req.args.day = player.user.activity.topone2.day;
    requestWorld(req, resp, function(){
        if( resp.code == 0 ) {
            var crazyRankConf = gConfAvCrazyRank['t1_'+player.user.activity.topone2.day];
            if( !crazyRankConf ) {
                crazyRankConf = gConfAvCrazyRank['t1_1'];
            }
            var awards = [crazyRankConf.Award1, crazyRankConf.Award2, crazyRankConf.Award3];
            awards = player.addAwards(awards);

            resp.data.awards = awards;
        }
        onHandled();
    });
}

exports.get_topone2_rank = function(player, req, resp, onHandled) {
    req.args.day = player.user.activity.topone2.day;
    requestWorld(req, resp, onHandled);
}

exports.get_topone2_rank_reward = function(player, req, resp, onHandled) {
    req.args.day = player.user.activity.topone2.day;
    requestWorld(req, resp, function(){
        if( resp.code == 0 ) {
            var awards = [];
            var ranks = resp.data.ranks;
            for( var type in ranks ) {
                var rewardType = type + ranks[type];
                var crazyRankConf = gConfAvCrazyRank[rewardType];
                awards.push(crazyRankConf.Award1);
                awards.push(crazyRankConf.Award2);
                awards.push(crazyRankConf.Award3);
            }
            awards = player.addAwards(awards);

            resp.data.awards = awards;
        }
        onHandled();
    });
}

exports.topone2_vote = function(player, req, resp, onHandled) {
    var topone2 = player.user.activity.topone2;

    if( topone2.vote >= gConfGlobal.CrazyRankSendTimes ) {
        resp.code = 1; resp.desc = 'not enough vote';
        onHandled();
        return;
    }

    requestWorld(req, resp, function(){
        if( resp.code == 0 ) {
            topone2.vote += 1;
            player.markDirty('activity.topone2.vote');

            // 随机获得魂,碎片
            var soul = common.randRange(gConfGlobal.CrazyRankSoulNumMin, 
                                        gConfGlobal.CrazyRankSoulNumMax);
            player.user.status.soul += soul; 
            player.markDirty('status.soul');
            
            var awards = [];
            var num = +common.wRand(gConfAvCrazySend);
            if( num > 0 ) {
                player.addInventory('material', 47, num);
                awards.push(['material', 47, num]);
            }

            awards.push(['user','soul', soul]); 

            resp.data.awards = awards;
            resp.data.vote = topone2.vote;
        }
        onHandled();
    });
}

exports.get_tower_activity_rank = function(player, req, resp, onHandled) {
    
    var user = player.user;
    req.args.name = user.info.un; 
    req.args.headpic = user.info.headpic;
    req.args.floor = user.tower.highest[1]; 

    requestWorld(req, resp, function(){
        onHandled();
    });
}

exports.get_tower_activity_rank_reward = function(player, req, resp, onHandled) {
    var activitySchedule = findActivitySchedule('tower', 2);
    if( !activitySchedule ) {
        resp.code = 1; 
        resp.desc = 'not open'; 
        onHandled();
        return; 
    }
        
    requestWorld(req, resp, function(){
        if( resp.code == 0 && !resp.data.update ) {
            resp.data.awards = player.addAwards(resp.data.awards);
        }

        onHandled();
    });
}

exports.get_level_rank = function(player, req, resp, onHandled) {

    var user = player.user;
    req.args.name = user.info.un; 
    req.args.headpic = user.info.headpic;
    req.args.level = user.status.level; 

    requestWorld(req, resp, function(){
        onHandled();
    });
}

exports.get_level_rank_reward = function(player, req, resp, onHandled) {
    var activitySchedule = findActivitySchedule('levelrank', 2);
    if( !activitySchedule ) {
        resp.code = 1; 
        resp.desc = 'not open'; 
        onHandled();
        return; 
    }

    requestWorld(req, resp, function(){
        if( resp.code == 0 && !resp.data.update ) {
            resp.data.awards = player.addAwards(resp.data.awards);
        }

        onHandled();
    });
}

exports.get_fight_force_rank = function(player, req, resp, onHandled) {

    var user = player.user;
    req.args.name = user.info.un; 
    req.args.headpic = user.info.headpic;
    req.args.fight_force = player.getFightForce();

    requestWorld(req, resp, function(){
        onHandled();
    });
}

exports.get_fight_force_rank_reward = function(player, req, resp, onHandled) {
    
    var activitySchedule = findActivitySchedule('fightforce', 2);
    if( !activitySchedule ) {
        resp.code = 1; 
        resp.desc = 'not open'; 
        onHandled();
        return; 
    }

    requestWorld(req, resp, function(){
        if( resp.code == 0 && !resp.data.update ) {
            resp.data.awards = player.addAwards(resp.data.awards);
        }

        onHandled();
    });
}
